#include <iostream>
#include <string>
#include <cstring>
using namespace std;

const int N = 1010;

int n, m, k;
string area[N];
const int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int vis[N][N];

void bfs() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (area[i][j] == 'g' && !vis[i][j]) {
                vis[i][j] = 1;
                for (int k = 0; k < 4; ++k) {
                    int xx = i + dir[k][0], yy = j + dir[k][1];
                    if (xx >= 0 && xx < n && yy >= 0 && yy < m && area[xx][yy] == '.') {
                        area[xx][yy] = 'g';
                        vis[xx][yy] = 1;
                    }
                }
            }
        }
    }
}

int main()
{
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        cin >> area[i];
    }

    cin >> k;
    for (int i = 0; i < k; ++i) {
        memset(vis, 0, sizeof vis);
        bfs();
    }
    for (int i = 0; i < n; ++i) {
        cout << area[i] << endl;
    }
    return 0;
}